package cn.gzy.controller;

import cn.gzy.dto.LoginUser;
import cn.gzy.pojo.User;
import cn.gzy.service.BlogService;
import cn.gzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Controller
public class publicController {
  @Autowired
  private UserService userService;
  @Autowired
  private BlogService blogService;
  private final String pre = "forward:/statics/html";
  private final String suf = ".html";


  @PostMapping("/login")
  @ResponseBody
  public Map<String, String> login(@RequestBody LoginUser user) {
    Map<String, String> response = new HashMap<>();

    // 调用用户服务进行登录验证
    String token = Optional.ofNullable(userService.login(user)).orElse("");

    if (token.isEmpty()) {
      response.put("message", "账号或密码错误"); // 返回错误信息
      return response;
    }

    response.put("token", token);
    return response;
  }



  @GetMapping({"/blog/*","/index","/personInfo/*","/person/*","/users/*","/login","/LoginReg"})
  public String common(HttpServletRequest req){
    System.out.println(pre + req.getRequestURI() + suf);
    // 去掉restful的参数，直接访问html文件
    return pre + "/"+req.getRequestURI().split("[/]")[1] + suf;
  }

  @GetMapping("/person/{id}")
  public User personal(HttpServletRequest req){
    Integer userId = (Integer) req.getAttribute("id");
    return userService.findByUserId(userId);
  }



}
